//
//  CYSegmentView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/14.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYSegmentView.h"
#import "CYLineBtn.h"
@interface CYSegmentView(){
    block _myblock;
    CGFloat _text_size;    //按钮字体大小
    CGFloat _button_width; //按钮的宽度
    UIColor *_select_color;  //选中的颜色
}
@property (nonatomic, strong) CYScrollView *scrollView;
@end
@implementation CYSegmentView

- (instancetype)initWithtitles:(NSArray*)titles size:(CGFloat )size selectColor:(UIColor *)color defaultSelectedIndex:(NSUInteger)index block: (block) callback{
    self = [super initWithColor:[UIColor whiteColor] line:LineBottom lineColor:ColorHex(0xf5f5f9)];
    if (self) {
        _myblock = callback;
        _text_size = size;
        _select_color = color;
        self.bounds = CGRectMake(0.0, 0.0, SCREEN_WIDTH, 44);
        self.lineWidth = 0.5;
        self.titles = titles;
        self.currentIndex = index;
    }
    return self;
}

- (CYScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[CYScrollView alloc]init];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = NO;
        [self addSubview:_scrollView];
        
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0));
        }];
    }
    return _scrollView;
}

- (void)setTitles:(NSArray *)titles{
    if (_titles != titles)
        _titles = titles;
    _button_width = (_titles.count < 10)?SCREEN_WIDTH / _titles.count : SCREEN_WIDTH / 5;//平均每个按钮的宽度
    [self initializeSubtitle];
}

- (void)setCurrentIndex:(NSUInteger)currentIndex{
    if (_currentIndex != currentIndex && currentIndex <= _titles.count){
        [self setupScrollViewContentOffset:currentIndex];
        _currentIndex = currentIndex;
        [self ergodic:_currentIndex + 200];
    }
}

- (void)setBounds:(CGRect)bounds{
    [super setBounds:bounds];
    self.scrollView.bounds = bounds;
}

//
- (void)setupScrollViewContentOffset:(NSUInteger)currentIndex{
    //向左滚动 || 向右滚动
    if (((currentIndex > _currentIndex && currentIndex > 2) ||(currentIndex < _currentIndex && currentIndex >= 2)) && currentIndex < _titles.count - 2 && _titles.count >= 10)//向左滚动
        _scrollView.offsetX = (currentIndex - 2) * _button_width;
}

- (void)initializeSubtitle{
    
    [self clearViews];
    
    for (int i = 0; i < _titles.count; i ++) {
        
        CYLineBtn *button = [[CYLineBtn alloc]initWithTitle:_titles[i] color:_select_color size:_text_size height:0.5];
        [button addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 200 + i;
        button.CYselected = (i == _currentIndex)?YES:NO;
        [self.scrollView addSubview:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(_button_width, self.bounds.size.height));
            make.top.equalTo(self.scrollView).with.offset(0);
            make.left.mas_equalTo(self.scrollView).with.offset(_button_width * i);
            if (i == _titles.count - 1)
                make.right.mas_equalTo(self.scrollView).with.offset(0);
        }];
    }
}

//设置
- (void)clickBtn:(CYLineBtn *)sender{
    [self ergodic:sender.tag];
    [self setupScrollViewContentOffset:sender.tag - 200];
    _currentIndex = sender.tag - 200;
    _myblock (_currentIndex);
}

- (void)ergodic:(NSUInteger)index{
    
    [_scrollView ergodicSubView:^(CYView *view) {
        CYLineBtn *button = (CYLineBtn *)view;
        if ([button isKindOfClass:[CYLineBtn class]])
            button.CYselected = (button.tag == index )?YES:NO;
    }];
    
}
//清除旧的子视图（按钮）
- (void)clearViews{
    [_scrollView ergodicSubView:^(CYView *view) {
        if (view.tag >= 200) {
            [view removeFromSuperview];
            view = nil;
        }
    }];
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
